﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmSoDuDauKy : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private KhachHangBOList khachHangBOList = null;
        private SoDuDauKyBOCollection soDuDauKyBOCollection = null;
        private string COLUMN_FIRST = "No1";
        private string COLUMN_SECOND = "Co1";
        #endregion variant

        //=====================================
        #region form
        public FrmSoDuDauKy()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmSoDuDauKy_Load);
            this.Shown += new EventHandler(FrmSoDuDauKy_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmSoDuDauKy_FormClosing);
            this.KeyDown += new KeyEventHandler(FrmSoDuDauKy_KeyDown);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
        }

        private void FrmSoDuDauKy_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                        Save();
                    else
                        if (this.btnEdit.Visible && ((e.KeyCode == Keys.E && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F2)))
                            Edit();
                        else
                            if (this.btnCancel.Visible && ((e.KeyCode == Keys.H && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F12)))
                                Cancel();
                            else
                                if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                                    this.numNam.Focus();
        }

        private void FrmSoDuDauKy_Shown(object sender, EventArgs e)
        {
            this.bandView.Focus();
        }

        private void FrmSoDuDauKy_Load(object sender, EventArgs e)
        {
            this.numNam.Value = DateTime.Now.Year;

            MyStyle.SetGridStyle(this.bandView, false);
            Utility.InitControl(this.repLookUpKhachHangID);

            RefreshData();
            SetFormStatus(this.formStatus);

            this.numNam.EditValueChanged += new EventHandler(numNam_EditValueChanged);
        }

        private void FrmSoDuDauKy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        private void numNam_EditValueChanged(object sender, EventArgs e)
        {
            FilterNam();
        }
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            this.khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.repLookUpKhachHangID, this.khachHangBOList, this.khachHangBOList.Count);

            FilterNam();
        }

        private void FilterNam()
        {
            int nam = decimal.ToInt32(this.numNam.Value);
            this.lblText.Text = "SỐ DƯ ĐẦU KỲ CỦA KHÁCH HÀNG VÀ NHÀ CUNG CẤP - NĂM " + nam;

            this.soDuDauKyBOCollection = SoDuDauKyBOCollection.GetSoDuDauKyBOCollection("Nam=" + nam);

            int i = 0;
            int length = this.khachHangBOList.Count;
            for (i = 0; i < length; i++)
                if (this.soDuDauKyBOCollection.HasKhachHang(this.khachHangBOList[i].Key) == false)
                {
                    SoDuDauKyBO soDuDauKyBO = SoDuDauKyBOCollection.NewSoDuDauKyBO();
                    soDuDauKyBO.Nam = nam;
                    soDuDauKyBO.KhachHangID = this.khachHangBOList[i].Key;
                    this.soDuDauKyBOCollection.Add(soDuDauKyBO);
                }

            this.bdSource.DataSource = this.soDuDauKyBOCollection.Clone();
            this.bandView.CheckLoaded();
            SetStt();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            string oldColumn = this.bandView.FocusedColumn.FieldName;
            string newColumn = (oldColumn == this.COLUMN_FIRST ? this.COLUMN_SECOND : this.COLUMN_FIRST);
            this.bandView.FocusedColumn = this.bandView.Columns[newColumn];

            this.bdSource.EndEdit();
            this.soDuDauKyBOCollection = (SoDuDauKyBOCollection)this.bdSource.DataSource;
            this.soDuDauKyBOCollection = this.soDuDauKyBOCollection.Save();

            this.bdSource.DataSource = this.soDuDauKyBOCollection.Clone();
            this.bandView.CheckLoaded();
            SetStt();

            this.bandView.FocusedColumn = this.bandView.Columns[oldColumn];
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSource.DataSource = this.soDuDauKyBOCollection.Clone();
            this.bandView.CheckLoaded();
            SetStt();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.bandView, edit);

            this.numNam.Enabled = !edit;

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            foreach (GridColumn col in this.bandView.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }

            this.bandView.Focus();

            if (edit)
            {
                string focusColumn = this.bandView.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST;

                this.bandView.FocusedColumn = this.bandView.Columns[focusColumn];
                this.bandView.ShowEditor();
            }
        }

        private void SetStt()
        {
            if (this.formStatus == FormStatus.Edit)
                this.bdSource.EndEdit();

            int stt = 0;
            int length = this.bandView.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.bandView.GetVisibleRowHandle(i);
                if (index >= 0)
                    this.bandView.SetRowCellValue(index, "STT0", ++stt);
            }
        }
        #endregion function
    }
}